<html>
<head charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>两个日期的月份差</title>
</head>
<script type="text/javascript">


    function check() {
        let date1 = document.getElementById("date1").value;
        let date2 = document.getElementById("date2").value;
        let number = monthsDiff(date1, date2);
        console.log("间隔月份=>" + number + "日期间隔=>" + dateDiff(date1, date2))
        if (number > 3) {
            console.log("只能查询连续三个月的信息")
        }
    }

    //返回两个日期相差的月数
    function monthsDiff(date1, date2) { //date1和date2是2019-3-12格式
        //用-分成数组
        date1 = date1.split("-");
        date2 = date2.split("-");
        //获取年,月数
        var year1 = parseInt(date1[0], 10),
            month1 = parseInt(date1[1], 10),
            year2 = parseInt(date2[0], 10),
            month2 = parseInt(date2[1], 10),
            //通过年,月差计算月份差
            months = (year2 - year1) * 12 + (month2 - month1) + 1;
        // 计算自然月
        return months;
    }

    //计算天数差的函数，通用
    function dateDiff(sDate1, sDate2) { //sDate1和sDate2是2019-3-12格式
        var aDate, oDate1, oDate2, iDays
        aDate = sDate1.split("-")
        oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为9-25-2017格式
        aDate = sDate2.split("-")
        oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
        //把相差的毫秒数转换为天数
        iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)
        return iDays
    }
</script>
<body>
<label for="date1">日期1</label>
<input type="date" id="date1" name="date1">

<label for="date2">日期1</label>
<input type="date" id="date2" name="date2">

<input type="button" value="提交" onclick="check()">
</body>
</html>